import { createStore } from 'redux'
import userStore from '../employeeList/userStore';


export default createStore((model = userStore, action) => {
  const { users } = model

  switch (action.type) {
    case 'create_user':
      const { user } = action.payload

      user.id = 'UID' + String(Math.floor(Math.random() * 1000000)).padStart(6, '0')

      return {
        users: users.concat([user]),
      }

    case 'delete_user':
      const { id } = action.payload.user

      return {
        users: users.filter(user => user.id !== id),
      }

    case 'update_user':
      const { user: newUser } = action.payload

      return {
        users: users.map(user => (user.id === newUser.id ? newUser : user)),
      }

    default:
      return model
  }
})
